<?php

use Workerman\Worker;
use Workerman\Lib\Timer;

include __DIR__ . '/vendor/autoload.php';
//创建一个变量存放所有房间
$connection_all = [];
$timer_all = [];
//$baseUrl = "http://192.168.15.131/";
$baseUrl = "http://api.big-iot.cn/";
//创建一个Worker监听127.0.0.1:16670, 使用websocket协议通讯
//$ws_worker = new Worker("websocket://0.0.0.0:16670");
define('HEARTBEAT_TIME', 60);
//$ws_worker = new Worker("websocket://0.0.0.0:16671");
$ws_worker = new Worker("websocket://0.0.0.0:82");

//启动4个进程对外提供服务
$ws_worker->count = 1;
$ws_worker->onConnect = function($connection) use($ws_worker, &$connection_all) {
    echo "\n" . $connection->getRemoteIp() . "连接服务器!\n";
};
//当接收到客户端发来的数据后显示数据并回发到客户端

$ws_worker->onMessage = function($connection, $data) use ($ws_worker, &$connection_all, &$timer_all) {
    global $baseUrl;
    $ip = $connection->getRemoteIp();
    if ($ip == '127.0.0.1') {
        $data = str_replace('\\"', '"', str_replace('\r\n', '', $data));
        $data = ltrim($data, '"');
        $data = rtrim($data, '"');
    }
    if ($data != '@') {
        echo "\n收到{$ip}发来消息\n";
        echo "you just received:     $data\n";
    }
    //判断数据是不是@
    if ($data == '@') $connection->send('@');
    //解析客户端发送过来的数据
    $dataInfo = json_decode($data, true);
    if (isset($dataInfo['account_id']))
        $connection_all[$dataInfo['account_id']] = $connection->id; //保存连接
//数据分发
    if ($dataInfo && isset($dataInfo['operation'])) {
        switch ($dataInfo['operation']) {
            case "CreateRoom"://创建房间
                $url = $baseUrl . "Xiangshan/CreateRoom";
                if(isset($dataInfo['account_id'])){
                    echo "account_id：{$dataInfo['account_id']}创建房间中……";
                }
                $httpData = https_post($data, $url);
                //给客户端推创建房间成功消息
                echo "得到参数:";
                var_dump($dataInfo);
                echo "\n返回：";
                var_dump($httpData);
                $connection->send($httpData);
                $httpData='';
                unset($httpData);
                break;
            case "PrepareJoinRoom"://有人成功进入房间围观，则向在线人员推送更新后的消息
                //请求控制器
                echo "接收时间：";
                var_dump(time());
                $url = $baseUrl . "Xiangshan/PrepareJoinRoom";
                $returnData = https_post($data, $url);
                //向客户端推送数据
                $connection->send($returnData);
                $returnData=json_decode($returnData);
                if(isset($returnData->result) && $returnData->result==0 && $returnData->operation=='GameOver'){
                    $button=['result'=>0, "operation"=>'GameOver','yupai'=>$returnData->players, 'result_message'=>'排行榜'];
                    //返回总的排行榜给所有用户
                    Sendplayers($returnData,$button);
                    //向围观者发送
                    SendWeiplayers($returnData,$button);
                }
                //断线重连用户进来了
                if(isset($returnData->result) && $returnData->result==0 && $returnData->operation=='GameStart'){
                    if(isset($returnData->players) && is_array($returnData->players)){
                        $returnData->operation='pull_GameStart';
                        foreach ($returnData->players as $player) {
                            $id=$player->id;
                            //给除了自己以外的玩家推自己的游戏信息
                            if (isset($connection_all[$id])) {
                                echo "\n准备给玩家: {$id} 推消息, 找到的连接是: {$connection_all[$id]}\n";
                                if ($ws_worker->connections[$connection_all[$id]]){
                                    $ws_worker->connections[$connection_all[$id]]->send(json_encode($returnData));
                                }
                            }
                        }
                    }
                    //清除流局倒计时器
                    if(isset($timer_all[10][$returnData->room_id])) Timer::del($timer_all[10][$returnData->room_id]);

                }

                $id='';
                $button='';
                $returnData = '';
                unset($returnData,$button,$id);
                echo "结束时间：";
                var_dump(time());
                break;
            case "AllGamerInfo"://获取所有人的信息
                //请求控制器
                echo "接收时间：";
                var_dump(time());
                $url = $baseUrl . "Xiangshan/AllGamerInfo";
                $returnData = https_post($data, $url);
                //向客户端推送数据
                $connection->send($returnData);
                $id='';
                $returnData = '';
                unset($returnData,$id);
                echo "结束时间：";
                var_dump(time());
                break;
            case "SitDown"://在房间坐下 另外给房主发一条开始的消息
                //请求控制器
                $url = $baseUrl . "Xiangshan/SitDown";
                $returnData = https_post($data, $url);
                $returnData=json_decode($returnData);
                var_dump(['SitDown_request' => $returnData]);
                if(isset($returnData->result) && $returnData->result==0 && $returnData->operation=='SitDown'){
                    $button=['result'=>0, "operation"=>'SitDown', 'data'=>$returnData->players, 'result_message'=>'我坐下了'];
                    Sendplayers($returnData,$button);
                }else{
                    $connection->send(json_encode($returnData));
                }

                $id='';
                $returnData = '';
                $button='';
                unset($returnData,$button,$id);
                break;
            case "StandUp"://站起围观
                //请求控制器
                $url = $baseUrl . "Xiangshan/StandUp";
                $returnData = https_post($data, $url);
                //向客户端推送数据
                $connection->send($returnData);
                $returnData=json_decode($returnData);
                if(isset($returnData->result) && $returnData->result==0 && $returnData->operation=='StandUp'){
                    if(isset($returnData->players) && is_array($returnData->players)){
                        $returnData->operation='pull_StandUp';
                        foreach ($returnData->players as $player) {
                            $id=$player->id;
                            //给除了自己以外的玩家推自己的游戏信息
                            if ($id != $dataInfo['account_id'] && isset($connection_all[$id])){
                                echo "\n准备给玩家: {$id} 推消息, 找到的连接是: {$connection_all[$id]}\n";
                                if ($ws_worker->connections[$connection_all[$id]]){
                                    $ws_worker->connections[$connection_all[$id]]->send(json_encode($returnData));
                                }
                            }
                        }
                    }
                }
                $id='';
                $returnData = '';
                unset($returnData,$id);
                break;
            case "GameStart"://游戏开始
                //请求控制器
                $url = $baseUrl . "Xiangshan/GameStart";
                $returnData = https_post($data, $url);
                //向客户端推送数据
                $connection->send($returnData);
                $returnData=json_decode($returnData);
                if(isset($returnData->result) && $returnData->result==0 && $returnData->operation=='GameStart'){
                    $returnData->operation = 'GameStart';
                    foreach ($returnData->players as $player) {
                        $id=$player->id;
                        //给除了自己以外的玩家推自己的游戏信息
                        // if (isset($connection_all[$id])) {
                        if (isset($connection_all[$id]) && $id != $dataInfo['account_id']) {
                            echo "\n准备给玩家: {$id} 推消息, 找到的连接是: {$connection_all[$id]}\n";
                            if ($ws_worker->connections[$connection_all[$id]]){
                                $ws_worker->connections[$connection_all[$id]]->send(json_encode($returnData));
                            }
                        }
                    }
                    SendWeiplayers($returnData,$returnData);
                    //向其他牌者发送庄家天胡的消息
                    if ($returnData->huplayers >= 0) {
                        $button = ['result' => 0, "operation" => 'pull_Can_Win', 'account_id' => $returnData->data->account_id, 'result_message' => '胡牌了'];
                        Sendplayers($returnData,$button);
                        SendWeiplayers($returnData,$button);
                    }

                    //开启一个自动托管定时器，10秒如果庄家没有打牌，系统直接抽一张牌出来自动打牌($roomid,$action,$account_id,$dpai,$kind,$timer_all)
                    //if($returnData->autoplay) {
                    //    if(isset($timer_all[2][$returnData->data->room_id])) Timer::del($timer_all[2][$returnData->room_id]);
                   //     $timer_all[2][$returnData->data->room_id]=Timer::add($returnData->timer, 'GameAutoPlaycard',[$returnData->data->room_id,2,$returnData->data->account_id,$returnData->players[0]->data->pai[0],0, $timer_all],false);
                  //  }
                    //开启一个定时器，XX秒如果没有打牌，直接流局
                  //  $timer_all[1][$returnData->data->room_id]=Timer::add($returnData->gameovertimer, 'GameOver',[$returnData->data->room_id, $returnData->data->account_id, $timer_all], false);

                }


                $id='';
                $returnData = '';
                $button='';
                unset($returnData,$button,$id);
                break;
            case "ShowCard"://玩家打牌
                isset($dataInfo['data']['room_id']) ? $room_id = $dataInfo['data']['room_id'] : $room_id = 0;
                //请求控制器
                $url = $baseUrl . "Xiangshan/PlayerShowCard";
                $returnData = https_post($data, $url);
                var_dump(['PlayerShowCard_request' => $returnData]);
                //向客户端推送数据
                $returnData = json_decode($returnData);
                if($returnData->type>0) {
                    $returnData->zhuang = json_decode(json_encode($returnData->zhuang), true);
                    if ($returnData->action == 1) {     //摸牌
                        $button = ['result' => 0, "operation" => 'pull_Mopai', 'data' => $returnData->data, 'index' => $returnData->index, 'action' => $returnData->action, 'yupai' => $returnData->yupai, 'result_message' => '摸牌回应'];
                        //向所有玩家推送我自己摸到的牌
                        Sendplayers($returnData, $button);
                        SendWeiplayers($returnData, $button);
                        //开启一个定时器，30秒如果没有打牌，直接流局
                        //$timer_all[1][$returnData->room_id] = Timer::add($returnData->gameovertimer, 'GameOver', [$returnData->room_id, $returnData->account_id, $timer_all], false);
                    } elseif ($returnData->action == 2) {  //打牌
                        //清除定时器
                        if (isset($timer_all[1][$returnData->room_id])) Timer::del($timer_all[1][$returnData->room_id]);
                        if(isset($timer_all[12][$returnData->room_id])) Timer::del($timer_all[12][$returnData->room_id]);
                        $button = ['result' => 0, "operation" => 'pull_Dapai', 'data' => $returnData->data, 'index' => $returnData->index, 'account_id' => $returnData->zhuang[$returnData->index], 'result_message' => '打牌回应'];
                        //向所有玩家发送自己打的的牌
                        Sendplayers($returnData, $button);
                        //向围观者发送自己打的的牌
                        SendWeiplayers($returnData, $button);
                        //吃碰杠胡
                        if (@$returnData->act) {
                            $returnData->act = json_decode(json_encode($returnData->act), true);
                            $button = ['result' => 0, "operation" => 'pull_Can_Do', 'wintype' => 2, 'data' => $returnData->data, 'act' => $returnData->act];
                            $button['result_message'] = '吃碰杠胡';
                            Sendplayers($returnData, $button);
                            SendWeiplayers($returnData, $button);
                            //开启一个定时器，五秒之后，如果大家都不操作，直接摸牌
                            //if (isset($timer_all[12][$returnData->room_id])) Timer::del($timer_all[12][$returnData->room_id]);
                            //$timer_all[12][$returnData->room_id] = Timer::add(5, 'GameGuo', [$returnData->room_id, $returnData->zhuang[$returnData->index], $timer_all], false);
                        }
                        //向所有玩家推送摸牌人的ID和摸到的牌
                        if ($returnData->mp > 0) {
                            if ($returnData->mopaiinfo->type == 1) {
                                $button = ['result' => 0, "operation" => 'pull_Mopai', 'data' => $returnData->mopaiinfo->data, 'index' => $returnData->mopaiinfo->index, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index], 'yupai' => $returnData->yupai];
                                //向玩家发送
                                Sendplayers($returnData, $button);
                                //向围观者发送
                                SendWeiplayers($returnData, $button);
                                //杠胡
                                if (@$returnData->mopaiinfo->act) {
                                    $returnData->mopaiinfo->act = json_decode(json_encode($returnData->mopaiinfo->act), true);
                                    $button = ['result' => 0, "operation" => 'pull_Can_Do', 'wintype' => 1, 'data' => $returnData->data, 'act' => $returnData->mopaiinfo->act, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index]];
                                    $button['result_message'] = '杠胡';
                                    Sendplayers($returnData, $button);
                                    SendWeiplayers($returnData, $button);
                                }
                            } elseif ($returnData->mopaiinfo->type == 2) {    //花牌
                                $button = ['result' => 0, "operation" => 'pull_Huapai', 'data' => $returnData->mopaiinfo->data, 'index' => $returnData->mopaiinfo->index, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index], 'yupai' => $returnData->yupai];
                                //向玩家发送
                                Sendplayers($returnData, $button);
                                //向围观者发送
                                SendWeiplayers($returnData, $button);

                                //种下一个定时器，继续摸牌去
                                if (isset($timer_all[11][$returnData->room_id])) Timer::del($timer_all[11][$returnData->room_id]);
                                $timer_all[11][$returnData->room_id] = Timer::add(2, 'GameMopai', [$returnData->room_id, $returnData->zhuang[$returnData->mopaiinfo->index], $timer_all], false);
                            }
                        }


                    } elseif ($returnData->action  == 3) {  //吃牌
                        //清除定时器
                        if (isset($timer_all[1][$returnData->room_id])) Timer::del($timer_all[1][$returnData->room_id]);
                        if(isset($timer_all[12][$returnData->room_id])) Timer::del($timer_all[12][$returnData->room_id]);
                        //向所有玩家发送自己吃的的牌
                        $button = ['result' => 0, "operation" => 'pull_Allchipai', 'data' => $returnData->data, 'onedata' => $returnData->onedata, 'disable_pai' => $returnData->disable_pai, 'index' => $returnData->index, 'account_id' => $returnData->zhuang[$returnData->index], 'result_message' => '吃牌啦'];
                        Sendplayers($returnData, $button);
                        //向围观者发送
                        SendWeiplayers($returnData, $button);
                        //开启一个定时器，XX秒如果没有打牌，直接流局
                       // $timer_all[1][$returnData->room_id] = Timer::add($returnData->gameovertimer, 'GameOver', [$returnData->room_id, $returnData->account_id, $timer_all], false);
                    } elseif ($returnData->action  == 4) {  //碰牌
                        //清除定时器
                        if (isset($timer_all[1][$returnData->room_id])) Timer::del($timer_all[1][$returnData->room_id]);
                        if(isset($timer_all[12][$returnData->room_id])) Timer::del($timer_all[12][$returnData->room_id]);
                        //向所有玩家发送自己碰的的牌
                        $button = ['result' => 0, "operation" => 'pull_Allpengpai', 'data' => $returnData->data, 'index' => $returnData->index, 'account_id' => $returnData->zhuang[$returnData->index], 'result_message' => '碰牌啦'];
                        Sendplayers($returnData, $button);
                        SendWeiplayers($returnData, $button);
                        //开启一个定时器，XX秒如果没有打牌，直接流局
                        //$timer_all[1][$returnData->room_id] = Timer::add($returnData->gameovertimer, 'GameOver', [$returnData->room_id, $returnData->account_id, $timer_all], false);
                    } elseif ($returnData->action  == 5 || $returnData->action  == 6 || $returnData->action  == 7) {  //点杠vs暗杠vs弯杠
                        $operation = [5 => 'pull_Alldiangang', 6 => 'pull_Allangang', 7 => 'pull_Allwangang'];
                        $wintype_row = [5 => 3, 6 => 4, 7 => 5];
                        //清除定时器
                        if (isset($timer_all[1][$returnData->room_id])) Timer::del($timer_all[1][$returnData->room_id]);
                        if(isset($timer_all[12][$returnData->room_id])) Timer::del($timer_all[12][$returnData->room_id]);
                        //向所有玩家发送自己点杠的的牌
                        $button = ['result' => 0, "operation" => 'pull_Alldiangang', 'data' => $returnData->data, 'index' => $returnData->index, 'account_id' => $returnData->zhuang[$returnData->index], 'result_message' => '点杠啦'];
                        $button['operation'] = $operation[$returnData->action];
                        Sendplayers($returnData, $button);
                        SendWeiplayers($returnData, $button);
                        //向所有玩家推送摸牌人的ID和摸到的牌
                        if ($returnData->mp > 0) {
                            if ($returnData->mopaiinfo->type == 1) {
                                $button = ['result' => 0, "operation" => 'pull_Mopai', 'data' => $returnData->mopaiinfo->data, 'index' => $returnData->mopaiinfo->index, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index], 'yupai' => $returnData->yupai];
                                //向玩家发送
                                Sendplayers($returnData, $button);
                                //向围观者发送
                                SendWeiplayers($returnData, $button);
                                //杠胡
                                if (@$returnData->mopaiinfo->act) {
                                    $returnData->mopaiinfo->act = json_decode(json_encode($returnData->mopaiinfo->act), true);
                                    $button = ['result' => 0, "operation" => 'pull_Can_Do', 'wintype' => $wintype_row[$returnData->action], 'data' => $returnData->data, 'act' => $returnData->mopaiinfo->act, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index]];
                                    $button['result_message'] = '杠胡';
                                    Sendplayers($returnData, $button);
                                    SendWeiplayers($returnData, $button);
                                }

                            } elseif ($returnData->mopaiinfo->type == 2) {    //花牌
                                $button = ['result' => 0, "operation" => 'pull_Huapai', 'data' => $returnData->mopaiinfo->data, 'index' => $returnData->mopaiinfo->index, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index], 'yupai' => $returnData->yupai];
                                //向玩家发送
                                Sendplayers($returnData, $button);
                                //向围观者发送
                                SendWeiplayers($returnData, $button);

                                //种下一个定时器，继续摸牌去
                                if (isset($timer_all[11][$returnData->room_id])) Timer::del($timer_all[11][$returnData->room_id]);
                                $timer_all[11][$returnData->room_id] = Timer::add(2, 'GameMopai', [$returnData->room_id, $returnData->zhuang[$returnData->mopaiinfo->index], $timer_all], false);
                            }
                        }
                        //开启一个定时器，XX秒如果没有打牌，直接流局
                       // $timer_all[1][$returnData->room_id] = Timer::add($returnData->gameovertimer, 'GameOver', [$returnData->room_id, $returnData->account_id, $timer_all], false);
                    } elseif ($returnData->action  == 8) {  //过
                        //清除定时器
                        if (isset($timer_all[1][$returnData->room_id])) Timer::del($timer_all[1][$returnData->room_id]);
                        //向所有玩家发送自己已经过了
                        $button = ['result' => 0, "operation" => 'pull_AllGuo', 'data' => $returnData->data, 'index' => $returnData->index, 'account_id' => $returnData->zhuang[$returnData->index], 'result_message' => '我过啦'];
                        Sendplayers($returnData, $button);
                        SendWeiplayers($returnData, $button);
                        if ($returnData->mp > 0) {
                            if ($returnData->mopaiinfo->type == 1) {
                                $button = ['result' => 0, "operation" => 'pull_Mopai', 'data' => $returnData->mopaiinfo->data, 'index' => $returnData->mopaiinfo->index, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index], 'yupai' => $returnData->yupai];
                                //向玩家发送
                                Sendplayers($returnData, $button);
                                //向围观者发送
                                SendWeiplayers($returnData, $button);
                                if (@$returnData->mopaiinfo->act) {
                                    $returnData->mopaiinfo->act = json_decode(json_encode($returnData->mopaiinfo->act), true);
                                    $button = ['result' => 0, "operation" => 'pull_Can_Do', 'wintype' =>6, 'data' => $returnData->data, 'act' => $returnData->mopaiinfo->act, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index]];
                                    $button['result_message'] = '杠胡';
                                    Sendplayers($returnData, $button);
                                    SendWeiplayers($returnData, $button);
                                }
                            } elseif ($returnData->mopaiinfo->type == 2) {    //花牌
                                $button = ['result' => 0, "operation" => 'pull_Huapai', 'data' => $returnData->mopaiinfo->data, 'index' => $returnData->mopaiinfo->index, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index], 'yupai' => $returnData->yupai];
                                //向玩家发送
                                Sendplayers($returnData, $button);
                                //向围观者发送
                                SendWeiplayers($returnData, $button);

                                //种下一个定时器，继续摸牌去
                                if (isset($timer_all[11][$returnData->room_id])) Timer::del($timer_all[11][$returnData->room_id]);
                                $timer_all[11][$returnData->room_id] = Timer::add(2, 'GameMopai', [$returnData->room_id, $returnData->zhuang[$returnData->mopaiinfo->index], $timer_all], false);
                            }
                        }
                        //开启一个定时器，XX秒如果没有打牌，直接流局
                       // $timer_all[1][$returnData->room_id] = Timer::add($returnData->gameovertimer, 'GameOver', [$returnData->room_id, $returnData->account_id, $timer_all], false);
                    } elseif ($returnData->action  == 9) {  //胡牌
                        //清除定时器
                        if (isset($timer_all[1][$returnData->room_id])) Timer::del($timer_all[1][$returnData->room_id]);
                        if(isset($timer_all[12][$returnData->room_id])) Timer::del($timer_all[12][$returnData->room_id]);
                        $button = ['result' => 0, "operation" => 'pull_Win', 'yupai' => $returnData->yupai, 'data' => $returnData->data, 'result_message' => '胡牌啦'];
                        Sendplayers($returnData, $button);
                        //向围观者发送
                        SendWeiplayers($returnData, $button);
                        //开启一个定时器，第二局开始
                        //Timer::add($returnData->gameautotimer, 'GameAutoStart', [$returnData->room_id, $returnData->account_id, $timer_all], false);
                    } elseif ($returnData->action  == 10) {  //荒局
                        //清除定时器
                        if (isset($timer_all[1][$returnData->room_id])) Timer::del($timer_all[1][$returnData->room_id]);
                        if(isset($timer_all[12][$returnData->room_id])) Timer::del($timer_all[12][$returnData->room_id]);
                        //$button = ['result' => 0, "operation" => 'pull_Huangju', 'data' => $returnData->data, 'index' => $returnData->mopaiinfo->index, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index], 'result_message' => '荒局啦'];
                        $button=$returnData;
                        Sendplayers($returnData, $button);
                        //向围观者发送
                        SendWeiplayers($returnData, $button);
                        //开启一个定时器，第二局开始
                        Timer::add($returnData->gameautotimer, 'GameAutoStart', [$returnData->room_id, $returnData->account_id, $timer_all], false);
                    }

                }else{
                    $connection->send(json_encode($returnData));
                }

                $button='';
                $returnData = '';
                $url = '';
                unset($returnData, $url,$button);
                break;
            case "Controller"://控制器发送
//向客户端推送数据
                if (isset($dataInfo['to'])) {
                    foreach ($dataInfo['to'] as $key => $value) {
                        if (isset($connection_all[$value])) {
                            if (isset($ws_worker->connections[$connection_all[$value]]))
                                $ws_worker->connections[$connection_all[$value]]->send(json_encode($dataInfo['data']));
                        }
                    }
                }
                break;
            case 'BroadcastVoice':
                BroadcastVoice($data);
                break;
            default:
                break;
//$returnData = json_encode($dataInfo);
        }
    }
};

$ws_worker->onClose = function($connection) use($ws_worker, &$connection_all, &$timer_all) {
    global $baseUrl;
    $conn_key = array_search($connection->id, $connection_all);
    echo 'leave line is'.$conn_key;
    $onlineUrl = $baseUrl."Xiangshan/GetOffLine";
    $returnData = https_post(json_encode(['account_id' => $conn_key, 'online_status' => 0]), $onlineUrl); //把玩家数据更新到数据库并且取得新的房间数据
    $returnData =json_decode($returnData);
    if(isset($returnData->result) && $returnData->result==0 && $returnData->operation=='GetOffLine'){
        if(isset($returnData->players) && is_array($returnData->players)){
            foreach ($returnData->players as $player) {
                $id=$player->id;
                //给在线的玩家推自己的游戏信息
                if (isset($connection_all[$id]) && $id != $conn_key){
                    echo "\n准备给玩家: {$id} 推消息, 找到的连接是: {$connection_all[$id]}\n";
                    if ($ws_worker->connections[$connection_all[$id]]){
                        $ws_worker->connections[$connection_all[$id]]->send(json_encode($returnData));
                    }
                }
            }
            $id='';
            unset($id);
        }
        //添加一个流局倒计时定时器
        if($returnData->data->room_status>=2){
            //开启一个定时器，XX秒如果没有打牌，直接流局
            $timer_all[1][$returnData->data->room_id]=Timer::add($returnData->data->gameovertimer, 'GameOver',[$returnData->data->room_id, $returnData->data->account_id, $timer_all], false);
        }
    }
    if ($conn_key) {
        $connection_all[$conn_key] = '';
        unset($connection_all[$conn_key]);
        $key = null;
        unset($conn_key);
    }
    $returnData='';
    unset($returnData);
    echo "\n onClose触发操作结束：\n";
};
//运行worker
$ws_worker->runAll();
function BroadcastVoice($data) {
    global $baseUrl;
    $url = $baseUrl.'Xiangshan/BroadcastVoice';
    $result = https_post($data, $url);
    var_dump(['BroadcastVoice_http_result' => $result]);
    $result = json_decode($result, true);
    if (is_array($result) && isset($result['to'])) {//如果能解析成功，即转发消息
        send2room($result['to'], json_encode($result['data']));
    }
}
//游戏结束
function GameOver($room_id,$account_id,$timer_all){
    global $ws_worker, $connection_all, $timer_all,$baseUrl;
    $url = $baseUrl.'Xiangshan/GameOver';
    $result = https_post(json_encode(['room_id' => $room_id,'account_id'=>$account_id,'time_all'=>$timer_all]), $url);
    $returnData = json_decode($result);
    $button=['result'=>0, "operation"=>'GameOver','data'=>$returnData->players, 'result_message'=>'排行榜'];
    //返回总的排行榜给所有用户
    Sendplayers($returnData,$button);
    //向围观者发送
    SendWeiplayers($returnData,$button);
}
//普通玩家发送专用函数
function Sendplayers($returnData,$button,$index=false)
{
    global $ws_worker, $connection_all, $timer_all, $baseUrl;
    if (isset($returnData->players) && is_array($returnData->players)) {
        foreach ($returnData->players as $player) {
            $id = $player->id;
            if($index){
                $index=$player->sort-1;
                if (isset($connection_all[$id]) && $index==$returnData->index) {
                    echo "\n准备给玩家: {$id} 推消息, 找到的连接是: {$connection_all[$id]}\n";
                    if ($ws_worker->connections[$connection_all[$id]]) {
                        $ws_worker->connections[$connection_all[$id]]->send(json_encode($button));
                    }
                }
            }else{
                if (isset($connection_all[$id])) {
                    echo "\n准备给玩家: {$id} 推消息, 找到的连接是: {$connection_all[$id]}\n";
                    if ($ws_worker->connections[$connection_all[$id]]) {
                        $ws_worker->connections[$connection_all[$id]]->send(json_encode($button));
                    }
                }
            }
        }
    }
    $id = '';
    $button = '';
    $returnData = '';
    unset($id, $button,$returnData);
}
//围观者发送专用函数
function SendWeiplayers($returnData,$button){
    global $ws_worker, $connection_all, $timer_all,$baseUrl;
    if(isset($returnData->weiplayers) && is_array($returnData->weiplayers)){
        foreach ($returnData->weiplayers as $players) {
            $id=$players->id;
            // if (isset($connection_all[$id]) && $index==$returnData->index) {
            if (isset($connection_all[$id])) {
                echo "\n准备给玩家: {$id} 推消息, 找到的连接是: {$connection_all[$id]}\n";
                if ($ws_worker->connections[$connection_all[$id]]){
                    $ws_worker->connections[$connection_all[$id]]->send(json_encode($button));
                }
            }
        }
    }
    $id = '';
    $button = '';
    $returnData = '';
    unset($id, $button,$returnData);
}
//过牌专用函数
function GameGuo($roomid,$account_id, $timer_all,$wintype=4){
    global $ws_worker, $connection_all, $timer_all,$baseUrl;
    echo "\nGameGuo,接收到的参数值：{$roomid}  " . time() . "\n";
    $url = $baseUrl.'Xiangshan/PlayerShowCard';
    $dataPost=[
        'room_id'=>$roomid,              //房间号
        'operation'=>'ShowCard',          //牌操作
        'action'=>8,                 //  操作种类
        'account_id'=>$account_id,         //  操作者id
        'cur_dpai'=>0,                //操作牌
        'kind'=>0,                      //吃碰杠种类
        'wintype'=>$wintype,                      //胡的种类
    ];
    var_dump($dataPost);
    echo time() . "\n";
    $url = $baseUrl.'Xiangshan/PlayerShowCard';
    $returnData = https_post(json_encode($dataPost), $url);
    var_dump(['GameMopai_result' => $returnData]);
    $returnData = json_decode($returnData);
    $returnData->zhuang = json_decode(json_encode($returnData->zhuang), true);

    $button = ['result' => 0, "operation" => 'pull_AllGuo', 'data' => $returnData->data, 'index' => $returnData->index, 'account_id' => $returnData->zhuang[$returnData->index], 'result_message' => '我过啦'];
    Sendplayers($returnData, $button);
    SendWeiplayers($returnData, $button);

    if($returnData->mopaiinfo->type==1) {
        $button = ['result' => 0, "operation" => 'pull_Mopai', 'data' => $returnData->mopaiinfo->data, 'index' => $returnData->mopaiinfo->index, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index], 'yupai' => $returnData->yupai];
        Sendplayers($returnData,$button);
        SendWeiplayers($returnData,$button);
        //杠胡
        if(@$returnData->mopaiinfo->act){
            $returnData->mopaiinfo->act=json_decode(json_encode($returnData->mopaiinfo->act),true);
            $button=['result'=>0, "operation"=>'pull_Can_Do','wintype'=>1, 'data'=>$returnData->data, 'act'=>$returnData->mopaiinfo->act,'account_id'=>$returnData->zhuang[$returnData->mopaiinfo->index]];
            $button['result_message']='杠胡';
            Sendplayers($returnData,$button);
            SendWeiplayers($returnData,$button);
        }
    }elseif($returnData->mopaiinfo->type==2){    //花牌
        $button = ['result' => 0, "operation" => 'pull_Huapai', 'data' => $returnData->mopaiinfo->data, 'index' => $returnData->mopaiinfo->index, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index], 'yupai' => $returnData->yupai];
        //向玩家发送
        Sendplayers($returnData,$button);
        SendWeiplayers($returnData,$button);
        //种下一个定时器，继续摸牌去
        if(isset($timer_all[11][$returnData->room_id])) Timer::del($timer_all[11][$returnData->room_id]);
        $timer_all[11][$returnData->room_id]=Timer::add(2, 'GameMopai',[$returnData->room_id, $returnData->zhuang[$returnData->mopaiinfo->index], $timer_all], false);
    }else{ //荒局啦
        //清除定时器
        if (isset($timer_all[1][$returnData->room_id])) Timer::del($timer_all[1][$returnData->room_id]);
        $button=$returnData;
        Sendplayers($returnData, $button);
        //向围观者发送
        SendWeiplayers($returnData, $button);
        //开启一个定时器，第二局开始
        Timer::add($returnData->gameautotimer, 'GameAutoStart', [$returnData->room_id, $returnData->account_id, $timer_all], false);

    }
    $id = '';
    $button = '';
    $returnData = '';
    unset($id, $button,$returnData);
}
//摸牌专用函数，
function GameMopai($roomid,$account_id, $timer_all){
    global $ws_worker, $connection_all, $timer_all,$baseUrl;
    echo "\n执行了GameMopai函数,接收到的参数值：{$roomid}  " . time() . "\n";
    $url = $baseUrl.'Xiangshan/PlayerShowCard';
    $dataPost=[
        'room_id'=>$roomid,              //房间号
        'operation'=>'ShowCard',          //牌操作
        'action'=>1,                 //  操作种类
        'account_id'=>$account_id,         //  操作者id
        'cur_dpai'=>0,                //操作牌
        'kind'=>0,                      //吃碰杠种类
        'wintype'=>0,                      //胡的种类
    ];
    var_dump($dataPost);
    echo time() . "\n";
    $url = $baseUrl.'Xiangshan/PlayerShowCard';
    $returnData = https_post(json_encode($dataPost), $url);
    var_dump(['GameMopai_result' => $returnData]);
    $returnData = json_decode($returnData);
    $returnData->zhuang = json_decode(json_encode($returnData->zhuang), true);
    if($returnData->mopaiinfo->type==1) {
        $button = ['result' => 0, "operation" => 'pull_Mopai', 'data' => $returnData->mopaiinfo->data, 'index' => $returnData->mopaiinfo->index, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index], 'yupai' => $returnData->yupai];
        Sendplayers($returnData,$button);
        SendWeiplayers($returnData,$button);
        //杠胡
        if(@$returnData->mopaiinfo->act){
            $returnData->mopaiinfo->act=json_decode(json_encode($returnData->mopaiinfo->act),true);
            $button=['result'=>0, "operation"=>'pull_Can_Do','wintype'=>1, 'data'=>$returnData->data, 'act'=>$returnData->mopaiinfo->act,'account_id'=>$returnData->zhuang[$returnData->mopaiinfo->index]];
            $button['result_message']='杠胡';
            Sendplayers($returnData,$button);
            SendWeiplayers($returnData,$button);
        }

        //10秒之后，自动执行打牌操作
        if ($returnData->autoplay) {
            if (isset($timer_all[2][$returnData->room_id])) Timer::del($timer_all[2][$returnData->room_id]);
            $timer_all[2][$returnData->room_id] = Timer::add($returnData->timer, 'GameAutoPlaycard', [$returnData->room_id, 2, $returnData->zhuang[$returnData->mopaiinfo->index], $returnData->mopaiinfo->data, 0, $timer_all], false);
        }


    }elseif($returnData->mopaiinfo->type==2){    //花牌
        $button = ['result' => 0, "operation" => 'pull_Huapai', 'data' => $returnData->mopaiinfo->data, 'index' => $returnData->mopaiinfo->index, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index], 'yupai' => $returnData->yupai];
        //向玩家发送
        if (isset($returnData->players) && is_array($returnData->players)) {
            foreach ($returnData->players as $player) {
                $id = $player->id;
                if (isset($connection_all[$id])) {
                    echo "\n准备给玩家: {$id} 推消息, 找到的连接是: {$connection_all[$id]}\n";
                    if ($ws_worker->connections[$connection_all[$id]]) {
                        $ws_worker->connections[$connection_all[$id]]->send(json_encode($button));
                    }
                }
            }
        }
        //向围观者发送
        if (isset($returnData->weiplayers) && is_array($returnData->weiplayers)) {
            foreach ($returnData->weiplayers as $players) {
                $id = $players->id;
                // if (isset($connection_all[$id]) && $index==$returnData->index) {
                if (isset($connection_all[$id])) {
                    echo "\n准备给玩家: {$id} 推消息, 找到的连接是: {$connection_all[$id]}\n";
                    if ($ws_worker->connections[$connection_all[$id]]) {
                        $ws_worker->connections[$connection_all[$id]]->send(json_encode($button));
                    }
                }
            }
        }
        //种下一个定时器，继续摸牌去
        if(isset($timer_all[11][$returnData->room_id])) Timer::del($timer_all[11][$returnData->room_id]);
        $timer_all[11][$returnData->room_id]=Timer::add(2, 'GameMopai',[$returnData->room_id, $returnData->zhuang[$returnData->mopaiinfo->index], $timer_all], false);
    }else{    //荒局啦
        $button=['result'=>0, "operation"=>'pull_Huangju','data'=>$returnData->data, 'index' => $returnData->mopaiinfo->index, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index], 'result_message'=>'荒局啦'];
        Sendplayers($returnData,$button);
        SendWeiplayers($returnData,$button);
        //开启一个定时器，第二局开始
        Timer::add($returnData->gameautotimer, 'GameAutoStart',[$returnData->room_id, $returnData->account_id, $timer_all], false);
    }
    $id = '';
    $button = '';
    $returnData = '';
    unset($id, $button,$returnData);
}
//游戏自动开始回调函数
function GameAutoStart($room_id,$account_id, $timer_all)
{
    global $ws_worker, $connection_all, $timer_all,$baseUrl;
    echo "\n执行了GameAutoStart函数,接收到的参数值：{$room_id}  " . time() . "\n";
    $url = $baseUrl.'Xiangshan/GameAutoStart';
    $dataPost = [
        'account_id' => $account_id,
        'operation' => 'GameAutoStart',
        'data' => [
            'room_id' => $room_id,
        ]
    ];
    $returnData = https_post(json_encode($dataPost), $url);
    var_dump(['GameAutoStart_result' => $returnData]);
    $returnData = json_decode($returnData);
    if (isset($returnData->result) && $returnData->result == 0 && $returnData->operation == 'GameStart') {
        if (isset($returnData->players) && is_array($returnData->players)) {
            $returnData->operation = 'GameStart';
            Sendplayers($returnData,$returnData);
            SendWeiplayers($returnData,$returnData);
            //向其他牌者发送庄家天胡的消息
            if ($returnData->huplayers >= 0) {
                $button = ['result' => 0, "operation" => 'pull_Can_Win', 'account_id' => $returnData->data->account_id, 'result_message' => '胡牌了'];
                Sendplayers($returnData,$button);
                SendWeiplayers($returnData,$button);
            }

        }
        //开启一个自动托管定时器，10秒如果庄家没有打牌，系统直接抽一张牌出来自动打牌($roomid,$action,$account_id,$dpai,$kind,$timer_all)
        if($returnData->autoplay) {
            if(isset($timer_all[2][$returnData->data->room_id])) Timer::del($timer_all[2][$returnData->data->room_id]);
            $timer_all[2][$returnData->data->room_id]=Timer::add(10, 'GameAutoPlaycard',[$returnData->data->room_id,2,$returnData->data->account_id,$returnData->players[0]->data->pai[0],0, $timer_all],false);
        }

    }
    if (isset($returnData->result) && $returnData->result == 0 && $returnData->operation == 'GameOver') {
        $button = ['result' => 0, "operation" => 'GameOver', 'yupai' => $returnData->players, 'result_message' => '排行榜'];
        //返回总的排行榜给所有用户
        Sendplayers($returnData,$button);
        SendWeiplayers($returnData,$button);
        $returnData = '';
        unset($returnData);

    }

}
/*游戏自动托管打牌
$dataPost=[
                'room_id'=>375619,  //房间号
                'operation'=>'ShowCard',  //牌操作
                'action'=>6,           //  暗杠
                'data'=>[
                    'index'=>1,//摸牌者id
                    'ppai'=>array(3,3,3,3)
                ]
            ];*/
function GameAutoPlaycard($roomid,$action,$account_id,$dpai,$kind,$timer_all,$wintype=0)
{
    global $ws_worker, $connection_all,$timer_all, $baseUrl,$connection;
    $dataPost=[
        'room_id'=>$roomid,              //房间号
        'operation'=>'ShowCard',          //牌操作
        'action'=>$action,                 //  操作种类
        'account_id'=>$account_id,         //  操作者id
        'cur_dpai'=>$dpai,                //操作牌
        'kind'=>$kind,                      //吃碰杠种类
        'wintype'=>$wintype,                      //胡的种类
    ];
    echo "\n执行了GameAutoPlaycard函数, " . time() . "\n";
    // var_dump($dataPost);
    echo time() . "\n";
    $url = $baseUrl.'Xiangshan/PlayerShowCard';
    $returnData = https_post(json_encode($dataPost), $url);
    var_dump(['GameAutoPlaycard_result' => $returnData]);
    $returnData = json_decode($returnData);
    //print_r($returnData);
    if($returnData->type>0) {
        $returnData->zhuang = json_decode(json_encode($returnData->zhuang), true);
        if($returnData->action ==2){
            //清除定时器
            if(isset($timer_all[12][$returnData->room_id])) Timer::del($timer_all[12][$returnData->room_id]);
            if(isset($timer_all[1][$returnData->room_id])) Timer::del($timer_all[1][$returnData->room_id]);
            $button=['result'=>0, "operation"=>'pull_Dapai', 'data'=>$returnData->data,'index'=>$returnData->index,'account_id'=>$returnData->zhuang[$returnData->index], 'result_message'=>'打牌回应'];
            //向所有玩家发送自己打的的牌
            Sendplayers($returnData,$button);
            //向围观者发送
            SendWeiplayers($returnData,$button);
            if(@$returnData->act){
                $returnData->act=json_decode(json_encode($returnData->act),true);
                $button=['result'=>0, "operation"=>'pull_Can_Do','wintype'=>2, 'data'=>$returnData->data, 'act'=>$returnData->act];
                $button['result_message']='吃碰杠胡';
                Sendplayers($returnData,$button);
                SendWeiplayers($returnData,$button);
                //开启一个定时器，五秒之后，如果大家都不操作，直接过牌
               // if(isset($timer_all[12][$returnData->room_id])) Timer::del($timer_all[12][$returnData->room_id]);
                //$timer_all[12][$returnData->room_id]=Timer::add(5, 'GameGuo',[$returnData->room_id, $returnData->zhuang[$returnData->mopaiinfo->index], $timer_all,4], false);

            }
            //向所有玩家推送摸牌人的ID和摸到的牌
            if($returnData->mp>0){
                if($returnData->mopaiinfo->type==1) {
                    $button = ['result' => 0, "operation" => 'pull_Mopai', 'data' => $returnData->mopaiinfo->data, 'index' => $returnData->mopaiinfo->index, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index], 'yupai' => $returnData->yupai];
                    //向玩家发送
                    Sendplayers($returnData,$button);
                    //向围观者发送
                    SendWeiplayers($returnData,$button);
                    //杠胡
                    if(@$returnData->mopaiinfo->act){
                        $returnData->mopaiinfo->act=json_decode(json_encode($returnData->mopaiinfo->act),true);
                        $button=['result'=>0, "operation"=>'pull_Can_Do','wintype'=>1, 'data'=>$returnData->data, 'act'=>$returnData->mopaiinfo->act,'account_id'=>$returnData->zhuang[$returnData->mopaiinfo->index]];
                        $button['result_message']='杠胡';
                        Sendplayers($returnData,$button);
                        SendWeiplayers($returnData,$button);
                        //开启一个定时器，五秒之后，如果大家都不操作，直接过牌
                        //if(isset($timer_all[12][$returnData->room_id])) Timer::del($timer_all[12][$returnData->room_id]);
                        //$timer_all[12][$returnData->room_id]=Timer::add(5, 'GameGuo',[$returnData->room_id, $returnData->zhuang[$returnData->mopaiinfo->index], $timer_all,3], false);
                    }

                }elseif($returnData->mopaiinfo->type==2){    //花牌
                    $button = ['result' => 0, "operation" => 'pull_Huapai', 'data' => $returnData->mopaiinfo->data, 'index' => $returnData->mopaiinfo->index, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index], 'yupai' => $returnData->yupai];
                    //向玩家发送
                    Sendplayers($returnData,$button);
                    //向围观者发送
                    SendWeiplayers($returnData,$button);
                    //种下一个定时器，继续摸牌去
                    if(isset($timer_all[11][$returnData->room_id])) Timer::del($timer_all[11][$returnData->room_id]);
                    $timer_all[11][$returnData->room_id]=Timer::add(2, 'GameMopai',[$returnData->room_id, $returnData->zhuang[$returnData->mopaiinfo->index], $timer_all], false);
                }else{    //荒局啦
                    $button=['result'=>0, "operation"=>'pull_Huangju','data'=>$returnData->data, 'index' => $returnData->mopaiinfo->index, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index], 'result_message'=>'荒局啦'];
                    Sendplayers($returnData,$button);
                    //向围观者发送
                    SendWeiplayers($returnData,$button);
                    //开启一个定时器，第二局开始
                    Timer::add($returnData->gameautotimer, 'GameAutoStart',[$returnData->room_id, $returnData->account_id, $timer_all], false);
                }
            }
            //开启一个定时器，XX秒如果没有打牌，直接流局
            //$timer_all[1][$returnData->room_id]=Timer::add($returnData->gameovertimer, 'GameOver',[$returnData->room_id, $returnData->account_id, $timer_all], false);

        }elseif($returnData->action ==3){  //吃牌
            //清除定时器
            if(isset($timer_all[1][$returnData->room_id])) Timer::del($timer_all[1][$returnData->room_id]);
            if(isset($timer_all[12][$returnData->room_id])) Timer::del($timer_all[12][$returnData->room_id]);
            //向所有玩家发送自己吃的的牌
            $button=['result'=>0, "operation"=>'pull_Allchipai', 'data'=>$returnData->data,'onedata'=>$returnData->onedata,'index'=>$returnData->index,'disable_pai' => $returnData->disable_pai,'account_id'=>$returnData->zhuang[$returnData->index] ,'result_message'=>'吃牌啦'];
            Sendplayers($returnData,$button);
            //向围观者发送
            SendWeiplayers($returnData,$button);
            //开启一个自动托管定时器，10秒如果没有打牌，直接自动打牌($roomid,$action,$account_id,$dpai,$kind,$timer_all)
            if($returnData->autoplay){
                if(isset($timer_all[2][$returnData->room_id])) Timer::del($timer_all[2][$returnData->room_id]);
                $timer_all[2][$returnData->room_id]=Timer::add($returnData->timer, 'GameAutoPlaycard',[$returnData->room_id,2,$returnData->zhuang[$returnData->index],$returnData->players[$returnData->index]->data->pai[0],0, $timer_all],false);
            }
            //开启一个定时器，XX秒如果没有打牌，直接流局
            $timer_all[1][$returnData->room_id]=Timer::add($returnData->gameovertimer, 'GameOver',[$returnData->room_id, $returnData->account_id, $timer_all], false);
        }elseif($returnData->action ==4){  //碰牌
            //清除定时器
            if(isset($timer_all[1][$returnData->room_id]))Timer::del($timer_all[1][$returnData->room_id]);
            if(isset($timer_all[12][$returnData->room_id])) Timer::del($timer_all[12][$returnData->room_id]);
            //向所有玩家发送自己碰的的牌
            $button=['result'=>0, "operation"=>'pull_Allpengpai', 'data'=>$returnData->data,'index'=>$returnData->index,'account_id'=>$returnData->zhuang[$returnData->index], 'result_message'=>'碰牌啦'];
            Sendplayers($returnData,$button);
            //向围观者发送
            SendWeiplayers($returnData,$button);
            //开启一个自动托管定时器，10秒如果没有打牌，直接自动打牌($roomid,$action,$account_id,$dpai,$kind,$timer_all)
            if($returnData->autoplay){
                if(isset($timer_all[2][$returnData->room_id])) Timer::del($timer_all[2][$returnData->room_id]);
                $timer_all[2][$returnData->room_id]=Timer::add($returnData->timer, 'GameAutoPlaycard',[$returnData->room_id,2,$returnData->zhuang[$returnData->index],$returnData->players[$returnData->index]->data->pai[0],0, $timer_all],false);
            }
            //开启一个定时器，XX秒如果没有打牌，直接流局
            $timer_all[1][$returnData->room_id]=Timer::add($returnData->gameovertimer, 'GameOver',[$returnData->room_id, $returnData->account_id, $timer_all], false);
        }elseif($returnData->action ==5 || $returnData->action ==6  || $returnData->action ==7){  //点杠
            //清除定时器
            if(isset($timer_all[1][$returnData->room_id])) Timer::del($timer_all[1][$returnData->room_id]);
            if(isset($timer_all[12][$returnData->room_id])) Timer::del($timer_all[12][$returnData->room_id]);
            $operation=[5=>'pull_Alldiangang',6=>'pull_Allangang',7=>'pull_Allwangang'];
            //向所有玩家发送自己点杠的的牌
            $button=['result'=>0, "operation"=>'pull_Alldiangang', 'data'=>$returnData->data,'index'=>$returnData->index,'account_id'=>$returnData->zhuang[$returnData->index], 'result_message'=>'点杠啦'];
            $button['operation']=$operation[$action];
            Sendplayers($returnData,$button);
            //向围观者发送
            SendWeiplayers($returnData,$button);
            //向所有玩家推送摸牌人的ID和摸到的牌
            if($returnData->mp>0){
                if($returnData->mopaiinfo->type==1) {
                    $button = ['result' => 0, "operation" => 'pull_Mopai', 'data' => $returnData->mopaiinfo->data, 'index' => $returnData->mopaiinfo->index, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index], 'yupai' => $returnData->yupai];
                    //向玩家发送
                    Sendplayers($returnData,$button);
                    //向围观者发送
                    SendWeiplayers($returnData,$button);
                    //杠胡
                    if(@$returnData->mopaiinfo->act){
                        $returnData->mopaiinfo->act=json_decode(json_encode($returnData->mopaiinfo->act),true);
                        $button=['result'=>0, "operation"=>'pull_Can_Do','wintype'=>3, 'data'=>$returnData->data, 'act'=>$returnData->mopaiinfo->act,'account_id'=>$returnData->zhuang[$returnData->mopaiinfo->index]];
                        $button['result_message']='杠胡';
                        Sendplayers($returnData,$button);
                        SendWeiplayers($returnData,$button);
                    }
                    //开启一个自动托管定时器，10秒如果没有打牌，直接自动打牌($roomid,$action,$account_id,$dpai,$kind,$timer_all)
                    if ($returnData->autoplay) {
                        if (isset($timer_all[2][$returnData->room_id])) Timer::del($timer_all[2][$returnData->room_id]);
                        $timer_all[2][$returnData->room_id] = Timer::add($returnData->timer, 'GameAutoPlaycard', [$returnData->room_id, 2, $returnData->zhuang[$returnData->mopaiinfo->index], $returnData->mopaiinfo->data, 0, $timer_all], false);
                    }
                }elseif($returnData->mopaiinfo->type==2){    //花牌
                    $button = ['result' => 0, "operation" => 'pull_Huapai', 'data' => $returnData->mopaiinfo->data, 'index' => $returnData->mopaiinfo->index, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index], 'yupai' => $returnData->yupai];
                    //向玩家发送
                    Sendplayers($returnData,$button);
                    //向围观者发送
                    SendWeiplayers($returnData,$button);
                    //种下一个定时器，继续摸牌去
                    if(isset($timer_all[11][$returnData->room_id])) Timer::del($timer_all[11][$returnData->room_id]);
                    $timer_all[11][$returnData->room_id]=Timer::add(2, 'GameMopai',[$returnData->room_id, $returnData->zhuang[$returnData->mopaiinfo->index], $timer_all], false);
                }
            }
            //开启一个定时器，XX秒如果没有打牌，直接流局
            $timer_all[1][$returnData->room_id]=Timer::add($returnData->gameovertimer, 'GameOver',[$returnData->room_id, $returnData->account_id, $timer_all], false);
        }elseif($returnData->action ==9){  //胡牌
            //清除定时器
            if(isset($timer_all[1][$returnData->room_id])) Timer::del($timer_all[1][$returnData->room_id]);
            if(isset($timer_all[12][$returnData->room_id])) Timer::del($timer_all[12][$returnData->room_id]);
            $button=['result'=>0, "operation"=>'pull_Win','yupai'=>$returnData->yupai,'data'=>$returnData->data, 'result_message'=>'胡牌啦'];
            Sendplayers($returnData,$button);
            //向围观者发送
            SendWeiplayers($returnData,$button);
            //开启一个定时器，第二局开始
            Timer::add($returnData->gameautotimer, 'GameAutoStart',[$returnData->room_id, $returnData->account_id, $timer_all], false);
        }elseif ($returnData->action== 10) {  //荒局
            //清除定时器
            if (isset($timer_all[1][$returnData->room_id])) Timer::del($timer_all[1][$returnData->room_id]);
            if (isset($timer_all[12][$returnData->room_id])) Timer::del($timer_all[12][$returnData->room_id]);
            //$button = ['result' => 0, "operation" => 'pull_Huangju', 'data' => $returnData->data, 'index' => $returnData->mopaiinfo->index, 'account_id' => $returnData->zhuang[$returnData->mopaiinfo->index], 'result_message' => '荒局啦'];
            $button = $returnData;
            Sendplayers($returnData, $button);
            //向围观者发送
            SendWeiplayers($returnData, $button);
            //开启一个定时器，第二局开始
            Timer::add($returnData->gameautotimer, 'GameAutoStart', [$returnData->room_id, $returnData->account_id, $timer_all], false);
        }
    }else{
        //$connection_all[$account_id]->send(json_encode($returnData));
    }
    $id = '';
    $button = '';
    $returnData = '';
    unset($id, $button,$returnData);
}
function https_post($data, $url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20); // 设置超时限制防止死循环
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8')
    );
    ob_start();
    curl_exec($ch);
    if (curl_errno($ch)) {
        return curl_error($ch); //捕抓异常
    }
    $return_content = ob_get_contents();
    ob_end_clean();
    curl_close($ch);
    return $return_content;
}
